package in.muton.edushare.components.tasks.invitations.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.dao.GroupsMembersDAO;
import com.edushare.common.dao.UserDeptLinkDAO;
import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class InvitationModel implements IModel<List<Invitation>> {
	private static final long serialVersionUID = -2948168803867516005L;

	public InvitationModel(IModel<Users> user) {
		userModel = user;
	}

	public List<Invitation> getObject() {
		List<Invitation> list = new ArrayList<Invitation>();

		List<UserRoles> uroles = new UserRolesDAO(EduShareSession.get()
				.getSessionORMInterface()).getOutstandingInvites(userModel
				.getObject());
		for(UserRoles r:uroles){
			list.add(new UnivInvitation(new CayenneModel<UserRoles>(r)));
		}
		
		List<UserDeptLink> udLink = new UserDeptLinkDAO(EduShareSession.get().getSessionORMInterface()).
			getOutstandingInvites(userModel.getObject());
		for(UserDeptLink r:udLink){
			list.add(new DeptInvitation(new CayenneModel<UserDeptLink>(r)));
		}
		
		List<Enrolments> eList = new EnrolmentsDAO(EduShareSession.get().getSessionORMInterface()).
			getOutstandingInvites(userModel.getObject());
		for(Enrolments r:eList){
			list.add(new CourseInvitation(new CayenneModel<Enrolments>(r)));
		}
		
		List<GroupMembers> groupInvites = new GroupsMembersDAO(EduShareSession.get().getSessionORMInterface()).
													getOutstandingInvites(userModel.getObject());
		for(GroupMembers gm:groupInvites){
			list.add(new GroupInvitations(new CayenneModel<GroupMembers>(gm)));
		}
		
		return list;
	}

	public void setObject(List<Invitation> arg0) {
	}

	public void detach() {
	}

	public int getNumber() {
		return getObject().size();
	}

	private IModel<Users> userModel;
}
